Add several error checkings in iterative version method and correct some boundary checks in recursive version one