Fix longstanding bug and add debugging.