fixing bug with creating loops