Added optional maximum query memory and re-planning.