Added commens and tightened logic