Better error handling and tests