Fixing some error handling issues