Refactored layouting, some bug fixes