cleaned up the code and added the multipart functionality to the write method