Improved masonry algorithm