final - added comments and finetuned code