improved performance by

* removing some unnecessary copying
* replacing element-wise copying with memcpy