added comments and fine tuned code