Refactored the code to be a little nicer, still more refactor that needs to be done