Fixed a bug when calculating offsets