upd
refactoring, bugfixes, python3 tested