Added input validation (fixes #1)