Refactored some duplicate code