Minor refactorings, added comments and descriptions.