Optimizations ideas from snoopyjs, freshly made maybe need corrections.