Refactor code into functions

I finally understood pointers, so I'm using some