refactored conditional code