827 refactoring

merge this after pull #833