Another implementation of masonry algorithm.