refactoring and add various comments