better file creation logic