Added function to calculate histogram