Some db query fixes