fixes and perf tweaks to mega query