hopefully works ok with python 2 and 3 (slightly horrible hack)