Refactored query builder to throw more semantic exceptions