Code refactoring, fix #8, #9