Major refactoring of my crappy codebase from many years ago