I have improved it using `rpartition` which is not perfect, but better.