Added some error handling and unit tests