python 3 version. TODO make work with either 2 or 3