Handling of output filenames can be made better

Still needs some work...