refactor logic for DRY