Added Debug function to handle debug messages, added OnSave&OnRestore