update to work with python3, fixes, refactoring