Fixed if statement throwing an exception