Refactor layout_by_resource