fix JSLint errors:

* see http://jslinterrors.com/dont-make-functions-within-a-loop and more
refractor code: nested sugQuery