Fix offset calculation and clean up math expressions