take some document queries out of js loops