Refactor and clean up: got rid of a loop