Some logic modification and some more comments to the code