Moved memory copying into a function