add a lot of code documentation and tweak a bit of the logic