Implemented detection algorithm with N last offsets

  mostly done with one last piece left after dinner