Optimization

see https://gist.github.com/eteubert/4054e4953104c9f84e90