Added python 3 compatibility and cleaned up code slightly.