small refactoring and fix #348