Added proper error handling and more unit tests