low-level optimization to reduce stack usage & accelerate calculations