big refactoring with api change & code cleanup