See comments written for details.