enhanced to handle file names containing spaces or newlines