tighten up tail recursive version, plus eliminate a bug