Minor refactoring
More refactoring and commenting