db query fixes & cleanup