add some comments to code; move complete handler to the separate function