code refactoring, added some docs