Fixed a crash when given invalid input

See comment added to code for clarification