Fix filename generation by replacing unsafe characters with underscores

Fixes #5 and #8