refactor all the method to avoid slow recursive algo