completely refactored the code to be modular, PITA