refactoring and pretty
rearrange all code into functions