Refactoring to provide better checks and fewer control structures