Fixed error handling bug