Fix margin calculation problem