loop and data structure changes to increase efficiency