Most code taken from the huge pull request f28674a68b and added another update, changed description.
f28674a68b