Fix filename generation by replacing unsafe characters with underscores

Fixes #5 (Also fixes #8 which is a duplicate).