diff --git a/src/libbson/CMakeLists.txt b/src/libbson/CMakeLists.txt
index 7c479d8..d383424 100644
--- a/src/libbson/CMakeLists.txt
+++ b/src/libbson/CMakeLists.txt
@@ -226,6 +226,9 @@ set (HEADERS_FORWARDING
    ${PROJECT_SOURCE_DIR}/src/bson/forwarding/bson.h
 )
 
+add_definitions(-D_CRT_SECURE_NO_WARNINGS)
+
+if (NOT ENABLE_STATIC MATCHES "ON|AUTO")
 add_library (bson_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
 set (CMAKE_CXX_VISIBILITY_PRESET hidden)
 target_compile_definitions (bson_shared PRIVATE BSON_COMPILATION JSONSL_PARSE_NAN)
@@ -268,9 +271,13 @@ if (WIN32)
    # must be handled specially since we can't resolve them
    set (BSON_SYSTEM_LIBRARIES ${BSON_SYSTEM_LIBRARIES} ws2_32)
 endif ()
+endif ()
 
 if (ENABLE_STATIC MATCHES "ON|AUTO")
    add_library (bson_static STATIC ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
+   set(CMAKE_CXX_VISIBILITY_PRESET hidden)
+   set(THREADS_PREFER_PTHREAD_FLAG 1)
+   find_package(Threads REQUIRED)
    target_compile_definitions (bson_static
       PUBLIC BSON_STATIC
       PRIVATE BSON_COMPILATION JSONSL_PARSE_NAN
@@ -284,6 +291,7 @@ if (ENABLE_STATIC MATCHES "ON|AUTO")
    if (RT_LIBRARY)
       target_link_libraries (bson_static ${RT_LIBRARY})
    endif ()
+   find_library(M_LIBRARY m)
    if (M_LIBRARY)
       target_link_libraries (bson_static ${M_LIBRARY})
    endif ()
@@ -298,7 +306,7 @@ function (add_example bin src)
    add_executable (${bin} ${BSON_EXAMPLE_SOURCES})
 
    # Link against the shared lib like normal apps
-   target_link_libraries (${bin} bson_shared)
+   target_link_libraries (${bin} bson_shared bson_static)
 
    set (EXAMPLES ${EXAMPLES} ${bin})
 endfunction ()
@@ -322,7 +330,7 @@ set (BSON_HEADER_INSTALL_DIR
 )
 
 if (ENABLE_STATIC MATCHES "ON|AUTO")
-   set (TARGETS_TO_INSTALL bson_shared bson_static)
+   set (TARGETS_TO_INSTALL bson_static)
 else ()
    set (TARGETS_TO_INSTALL bson_shared)
 endif ()
@@ -337,6 +345,7 @@ install (
 )
 
 if (MSVC)
+   if (NOT ENABLE_STATIC MATCHES "ON|AUTO")
    add_custom_command (
       TARGET bson_shared
       POST_BUILD
@@ -348,6 +357,7 @@ if (MSVC)
       FILES $<TARGET_LINKER_FILE_DIR:bson_shared>/lib$<TARGET_LINKER_FILE_NAME:bson_shared>
       DESTINATION ${CMAKE_INSTALL_LIBDIR}
    )
+   else()
    if (TARGET bson_static)
       add_custom_command (
          TARGET bson_static
@@ -361,6 +371,7 @@ if (MSVC)
          DESTINATION ${CMAKE_INSTALL_LIBDIR}
       )
    endif ()
+   endif()
 endif ()
 
 install (
diff --git a/src/libmongoc/CMakeLists.txt b/src/libmongoc/CMakeLists.txt
index 0f9e50c..85eb429 100644
--- a/src/libmongoc/CMakeLists.txt
+++ b/src/libmongoc/CMakeLists.txt
@@ -694,6 +694,7 @@ if (WIN32)
    set (LIBRARIES ${LIBRARIES} ws2_32)
 endif ()
 
+if (NOT MONGOC_ENABLE_STATIC)
 add_library (mongoc_shared SHARED ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
 set_target_properties (mongoc_shared PROPERTIES CMAKE_CXX_VISIBILITY_PRESET hidden)
 target_link_libraries (mongoc_shared PRIVATE ${LIBRARIES} PUBLIC ${BSON_LIBRARIES})
@@ -704,6 +705,7 @@ target_compile_definitions (mongoc_shared PRIVATE MONGOC_COMPILATION)
 
 set_target_properties (mongoc_shared PROPERTIES VERSION 0.0.0 SOVERSION 0)
 set_target_properties (mongoc_shared PROPERTIES OUTPUT_NAME "${MONGOC_OUTPUT_BASENAME}-${MONGOC_API_VERSION}")
+endif ()
 
 if (MONGOC_ENABLE_STATIC)
    add_library (mongoc_static STATIC ${SOURCES} ${HEADERS} ${HEADERS_FORWARDING})
@@ -731,7 +733,11 @@ if (ENABLE_APPLE_FRAMEWORK)
 endif ()
 
 add_executable (mongoc-stat ${PROJECT_SOURCE_DIR}/../../src/tools/mongoc-stat.c)
+if (MONGOC_ENABLE_STATIC)
+target_link_libraries (mongoc-stat mongoc_static)
+else ()
 target_link_libraries (mongoc-stat mongoc_shared ${LIBRARIES})
+endif ()
 
 # mongoc-stat works if shared memory performance counters are enabled.
 if (ENABLE_SHM_COUNTERS STREQUAL "ON")
@@ -977,7 +983,7 @@ file (COPY ${PROJECT_SOURCE_DIR}/tests/x509gen DESTINATION ${PROJECT_BINARY_DIR}
 file (COPY ${PROJECT_SOURCE_DIR}/tests/release_files DESTINATION ${PROJECT_BINARY_DIR}/tests)
 
 if (MONGOC_ENABLE_STATIC)
-   set (TARGETS_TO_INSTALL mongoc_shared mongoc_static)
+   set (TARGETS_TO_INSTALL mongoc_static)
 else ()
    set (TARGETS_TO_INSTALL mongoc_shared)
 endif ()
@@ -997,6 +1003,7 @@ install (
 )
 
 if (MSVC)
+   if (NOT ENABLE_STATIC MATCHES "ON|AUTO")
    add_custom_command (
       TARGET mongoc_shared
       POST_BUILD
@@ -1008,6 +1015,7 @@ if (MSVC)
       FILES $<TARGET_LINKER_FILE_DIR:mongoc_shared>/lib$<TARGET_LINKER_FILE_NAME:mongoc_shared>
       DESTINATION ${CMAKE_INSTALL_LIBDIR}
    )
+   else()
    if (TARGET mongoc_static)
       add_custom_command (
          TARGET mongoc_static
@@ -1021,6 +1029,7 @@ if (MSVC)
          DESTINATION ${CMAKE_INSTALL_LIBDIR}
       )
    endif ()
+   endif()
 endif ()
 
 install (
diff --git a/src/libmongoc/src/mongoc/mongoc-gridfs-file.c b/src/libmongoc/src/mongoc/mongoc-gridfs-file.c
index 0690b86..98721df 100644
--- a/src/libmongoc/src/mongoc/mongoc-gridfs-file.c
+++ b/src/libmongoc/src/mongoc/mongoc-gridfs-file.c
@@ -765,7 +765,7 @@ _mongoc_gridfs_file_refresh_page (mongoc_gridfs_file_t *file)
    bson_t query;
    bson_t child;
    bson_t opts;
-   const bson_t *chunk;
+   const bson_t *chunk = NULL;
    const char *key;
    bson_iter_t iter;
    int64_t existing_chunks;
diff --git a/src/libmongoc/src/mongoc/mongoc-handshake.c b/src/libmongoc/src/mongoc/mongoc-handshake.c
index 8374af2..5491595 100644
--- a/src/libmongoc/src/mongoc/mongoc-handshake.c
+++ b/src/libmongoc/src/mongoc/mongoc-handshake.c
@@ -286,7 +286,8 @@ _get_os_version (void)
    char *ret = bson_malloc (HANDSHAKE_OS_VERSION_MAX);
    bool found = false;
 
-#ifdef _WIN32
+#if defined (WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
+#elif defined (_WIN32)
    OSVERSIONINFO osvi;
    ZeroMemory (&osvi, sizeof (OSVERSIONINFO));
    osvi.dwOSVersionInfoSize = sizeof (OSVERSIONINFO);
diff --git a/src/libmongoc/src/mongoc/mongoc-util.c b/src/libmongoc/src/mongoc/mongoc-util.c
index 99939ff..3c25e19 100644
--- a/src/libmongoc/src/mongoc/mongoc-util.c
+++ b/src/libmongoc/src/mongoc/mongoc-util.c
@@ -84,7 +84,11 @@ _mongoc_hex_md5 (const char *input)
 void
 _mongoc_usleep (int64_t usec)
 {
-#ifdef _WIN32
+#if defined (WINAPI_FAMILY_PARTITION) && !WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP | WINAPI_PARTITION_SYSTEM)
+   if (usec / 1000 > MAXDWORD || usec < 0)
+      __fastfail(-1);
+   Sleep((DWORD)(usec / 1000));
+#elif defined(_WIN32)
    LARGE_INTEGER ft;
    HANDLE timer;
 
