My fix for "Invalid key to next" error.