low-level optimization to reduce stack usage & accelerate calculations

+ added function recursion exception code from Paul Torsten