Rewrote queries to improve performance