Organizing code and adding some comments