Fixed bug when calculating position