refactored if to reduce nesting