Improve some of the path to filename code

Also use it more often to be more cross platform.