more performance improvements, rewritten queries