Moved complex logic out of if clause