more robust lastsplit function