added comments to complex code