Rewrote some SQL statements.