Refactor a bit and compute vertical margins as well.