Rewrote query structure