rewrote in a sane structure, for python2