Optimization suggested by @nbeloglazov