Refactored & tested code for populating term frequency