Some refactoring and documentation additions