Major refactor into C++ classes