adding saving function,fixed bugs