Some algorithm tweaks
merge conflicts