Updated path calculation to end at the last query.