moved all file handling logic to handler()