Summary: (i) cleaning, (ii) centroid calculation.

Cleaned up the code and added a bunch of comments. Also, added a centroid function instead of the other one that found the center at the center of the bounding box.