Add a little more error handling and tests