And fixed the first algorithm correctly this time.