Heavy optimization of embedded-shapes handling

Not nearly ready to be merged, but looking promising.