Commented the code, improved clarity