refactored save and fixed bugs