Fixes portable query issues

Fixes #2545
Fixes #2514