Further work on vectorization. Works but not yet finished.