fixing margin calculation, it was not using the scale factor