fixed reference issues valarray, mem issue freed pointer