Fix query and add logic to UI