some re-organization and refactoring