Fix offset calculation bug