Replaced some of the code with helper functions