Refactored code as it was larger than it needed to be.