replace nested conditional with guard

Credit to Myron Marston for suggestion.