Add error handling and tests