Works for the general cases, sometimes trouble with last index and 1st index