Massive refactor to put everything in its own file