Some code cleanup, requiring less lines and addition of some comments to make the understanding of the flow easier.