Test new query and optimise code