ugly but useful query update (hopefully I make it pretty later)