some API refactoring, incomplete.