move preprocessing out of loop for readability