Added comments. Minor refactoring.