added missing offset calculation in loop