Add error checking and file permissions