Better state and error handling
Add option to set file modes