Added detailed error catching to write process

Also reorganized some minor things and added some comments. Still need
to allow for continuing despite errors when scanning directories.