Cleanup code. Fix query so it correctly does check.