only query once, keep output for later use

This dramatically speeds up the script since we only query once and keep the info for later use.