Fix query construction