Rewrote parts to work well with python3.