Refactored out the code so it was cleaner and created some private methods in the process