More optimizations for shape:360

Function is now 8 times faster. It fails with very small images, however.