fixed typeOf fn that was broken by external contribution, optimized original code for speed and size