Improved filename sanitation.
Fixes #656