Layout of rectangles refacted and some errors fixed