Fixing bug when next flag was set to null