Reduced the code more by putting a reused if statement into a function.