Saving and flagging function