Adjusted to work for Python3 as well.