Optimizations

Work in progress, several attempts of optimizations
Also adds unit testing and primitive benchmark module.