Rewrote some db queries for dsr.