In preparation for Python 3 support.

Rework how much of lock/threading is done,
make syntax both 2 and 3 compatible.