Cleaned up the code a bit to make it C++ compatible