Refactored code by inverting if statements and removing redundant if
statements.