Cleanup code - reorder checks and make variable declaration scope narrower

This addresses minor issues:
- makes cheaper checks run first
- omits redundant actions when we return anyway
- declares a variable such that it's clear that it's variable is not persistent between usages and it's always initialized