Refactored code to less lines and less functions.