added tests, improved error handling