Moved some code into an if statment