Add docs. Minor refactoring.