Improve data structure to eliminate looping