major rewrite of DB queries