Improve perf by removing a loop that does IO