Modified code for saving structures