Fixed C++03 problems and refactored the code