Improved error handling of export and added API route to initiate it