refactor long if into smaller