Rearranged so that QUEST is implementable as a function