Clean up. Add documentation. Minor code refactoring.