added memory efficient gram making