Refactored some duplicate code.