Issue1808 pull request

refactoring and some fixing