Optimized version, removed dead code.