Refactored some more complex functions.