move all code under lib directory and small refactoring