Write first tests, fix a longstanding bug