refactor all the shit out of this