Fixes for complex queries