fixed both loop problems