math: handle offset calculation better