changed code to create term dictionary rather than collect terms themselves