cleaned up saving function