Refactor

Move functionality into functions
Update variable names