Hoist the variables in the picturefill function

This avoids confusion in the logic about the state of the variables
at the start of the loop which was causing a bug.