Commenting code and applying some minor refactoring