refactoring to consolidate code