use Qt's foreach macro in some places when const-ness is fine.
some cleanup/optomization.