version 2 rewritten in Python