added missing code
included optional history_sync feature, more comments