Index: wp-app.php
===================================================================
--- wp-app.php	(revision 16019)
+++ wp-app.php	(working copy)
@@ -387,7 +387,7 @@
 	 * @since 2.2.0
 	 */
 	function create_post() {
-		global $blog_id, $user_ID;
+		global $user_ID;
 		$this->get_accepted_content_type($this->atom_content_types);
 
 		$parser = new AtomParser();
@@ -419,7 +419,7 @@
 		if ( !current_user_can($cap) )
 			$this->auth_required(__('Sorry, you do not have the right to edit/publish new posts.'));
 
-		$blog_ID = (int ) $blog_id;
+		$blog_ID = get_current_blog_id();
 		$post_status = ($publish) ? 'publish' : 'draft';
 		$post_author = (int) $user_ID;
 		$post_title = $entry->title[1];
Index: wp-includes/admin-bar/admin-bar-class.php
===================================================================
--- wp-includes/admin-bar/admin-bar-class.php	(revision 16019)
+++ wp-includes/admin-bar/admin-bar-class.php	(working copy)
@@ -6,7 +6,7 @@
 	var $changed_locale = false;
 
 	function WP_Admin_Bar() {
-		global $current_user, $blog_id;
+		global $current_user;
 
 		$this->user = new stdClass;
 		$this->menu = new stdClass;
@@ -18,7 +18,7 @@
 			$this->user->domain = empty( $this->user->active_blog ) ? user_admin_url() : trailingslashit( get_home_url( $this->user->active_blog->blog_id ) );
 			$this->user->account_domain = $this->user->domain;
 		} else {
-			$this->user->active_blog = $this->user->blogs[$blog_id];
+			$this->user->active_blog = $this->user->blogs[get_current_blog_id()];
 			$this->user->domain = trailingslashit( home_url() );
 			$this->user->account_domain = $this->user->domain;
 		}
@@ -71,7 +71,7 @@
 	function remove_menu( $id ) {
 		return $this->remove_node( $id, $this->menu );
 	}
-	
+
 	function render() {
 	?>
 		<div id="wpadminbar" class="snap_nopreview no-grav">
Index: wp-includes/general-template.php
===================================================================
--- wp-includes/general-template.php	(revision 16019)
+++ wp-includes/general-template.php	(working copy)
@@ -492,6 +492,18 @@
 }
 
 /**
+ * Retrieve the current blog id
+ *
+ * @since 3.1.0
+ *
+ * @return int Blog id
+ */
+function get_current_blog_id() {
+	global $blog_id;
+	return absint($blog_id);
+}
+
+/**
  * Display or retrieve page title for all areas of blog.
  *
  * By default, the page title will display the separator before the page title,
Index: wp-includes/post.php
===================================================================
--- wp-includes/post.php	(revision 16019)
+++ wp-includes/post.php	(working copy)
@@ -1016,7 +1016,7 @@
  *   behave like edit_posts.
  * - publish_posts - Controls publishing objects of this post type.
  * - read_private_posts - Controls whether private objects can be read.
- 
+
  * These four primitive capabilities are checked in core in various locations.
  * There are also seven other primitive capabilities which are not referenced
  * directly in core, except in map_meta_cap(), which takes the three aforementioned
@@ -2265,11 +2265,11 @@
 		'meta_value' =>'', 'post_type' => 'post', 'post_status' => 'draft, publish, future, pending, private',
 		'suppress_filters' => true
 	);
-	
+
 	$r = wp_parse_args( $args, $defaults );
 
 	$results = get_posts( $r );
-	
+
 	// Backward compatibility. Prior to 3.1 expected posts to be returned in array
 	if ( ARRAY_A == $output ){
 		foreach( $results as $key => $result ) {
@@ -4141,7 +4141,7 @@
  * @return string The date.
  */
 function _get_last_post_time( $timezone, $field ) {
-	global $wpdb, $blog_id;
+	global $wpdb;
 
 	if ( !in_array( $field, array( 'date', 'modified' ) ) )
 		return false;
@@ -4152,7 +4152,7 @@
 
 	$post_types = apply_filters( "get_lastpost{$field}_post_types", (array) $post_types );
 
-	$key = "lastpost{$field}:$blog_id:$timezone:" . md5( serialize( $post_types ) );
+	$key = "lastpost{$field}:" . get_current_blog_id() . ":$timezone:" . md5( serialize( $post_types ) );
 
 	$date = wp_cache_get( $key, 'timeinfo' );
 
Index: wp-includes/user.php
===================================================================
--- wp-includes/user.php	(revision 16019)
+++ wp-includes/user.php	(working copy)
@@ -376,7 +376,7 @@
 			$this->query_vars = wp_parse_args( $query, array(
 				'blog_id' => $GLOBALS['blog_id'],
 				'role' => '',
-				'meta_key' => '', 
+				'meta_key' => '',
 				'meta_value' => '',
 				'meta_compare' => '',
 				'include' => array(),
@@ -572,10 +572,8 @@
  * @return array List of users that are part of that Blog ID
  */
 function get_users_of_blog( $id = '' ) {
-	global $blog_id;
-
 	if ( empty( $id ) )
-		$id = (int) $blog_id;
+		$id = get_current_blog_id();
 
 	return get_users( array( 'blog_id' => $id ) );
 }
@@ -593,7 +591,7 @@
 	global $wpdb;
 
 	if ( !is_multisite() ) {
-		global $blog_id;
+		$blog_id = get_current_blog_id();
 		$blogs = array();
 		$blogs[ $blog_id ]->userblog_id = $blog_id;
 		$blogs[ $blog_id ]->blogname = get_option('blogname');
@@ -757,10 +755,10 @@
  * @return array Includes a grand total and an array of counts indexed by role strings.
  */
 function count_users($strategy = 'time') {
-	global $wpdb, $blog_id, $wp_roles;
+	global $wpdb, $wp_roles;
 
 	// Initialize
-	$id = (int) $blog_id;
+	$id = get_current_blog_id();
 	$blog_prefix = $wpdb->get_blog_prefix($id);
 	$result = array();
 
