Improve inline documentation and refactoring