Switched it to functions and cleaned up the code a bit more