Refactored some methods to reduce complexity.