Minor refactoring and added some comments