refactored to make it python 3.3 compatible