Rewritten for MSVC 2012 C89