saving functionality, fixed few bugs