more refactoring to reduce duplication