[Refatroing] used stack instead of list