all is fixed, small optimisation from original