finalizing the code and added comments