refactoring, separate most code to lib
added tests
recent