Fix bug that led to paths sometimes ending with NaN