Fixed error handling issues