almost done refactoring massive code
seems to compile and work now