added support for queries, cleaned up entity builder logic