refactored the bejezus out of it