rewrite much of the layout logic, it was pretty broken before