Fix typos and add Python 3 support

This provides a pretty minimal diff regarding Python 3, keeping your itertools-based functional style where feasible.