Fix tests and error handling