Improved code and added code comments