reduce unnecessary mem copies
modified sample