reorder queries so script actually works