Refactored some methods to improve performance.