simplify logic and correct margins when perfect margin mode is disabled