Cleaning up local mess
Replace naive impl with flat replacement style
Done optimizing flat structure