Implementation and optimizations