Minor modifications to the code, and added comments