advanced code refactoring, add comments