Refactoring and fix #2