Added input validation and tests for it.