Final version by choko, not submitted previously to github