Major performance improvements in core loop. (Around 140x faster, thanks
to preallocation and in-place updates)