fixing bug when last is not defined