non recursive approach bug was fixed