Added some better error handling, minor updates to logging, optimized code and database calls so that attachments are saved only before being uploaded