low-level optimization to reduce stack usage & accelerate calculations

also added function recursion exception code from Paul Torsten