Big code cleanup, including widespread use of the new custom memory
allocator and fixing the code to be compilable as C++.