Moved if statement out for cleaner code