Some enhancement and refactoring