changed function: non recursive version