Some refactoring and inline documentation