fixing error handling bug