Refactor to clean up duplicate code