compact some query logic
more tweaks related to sqlite interaction