Improved input validation, added more tests and documentation.