Reworked and commented for improved use/understanding