replace two heap allocations with stack