Refactoring and clean up of internal API