Cleaning up code + adding some comments