Wrote a working code with comments