code cleanup and python3 support