Fix return code and error handling