A lot of fixes and refactoring
Large refactoring
Fixes for VS 2010 and GCC 4.8.2 in C++03 mode