Various improvements, refactoring, and documentation