Minor refactoring, lots of doccomments added