Refactored inner loop for creating rec.hist