fix #5, fix #6, minor refactoring, add test script