refactor offset calculation