Improved version. Multiple calling indexOf functions was deleted.