Some more refactoring, adding documentation.