Minor refactoring to reduce clutter

too many nested ifs, puts the error condition in one spot