added removal of elements after entering the top