fix an issue onsave and cleanups
added test cases