Adding some more error handling and tests