Thanks to asgeirn: input validation, fixed warning