fix error handling bug