Polishing the code and adding some comments