refactored scalar query functions to share common code