Try to fix #1763 and a small refactoring
Merge pull request #9 from alexvins/issue/1763

Try to fix #1763 and a small refactoring