more readability fixes - localize some variables and invert