More commenting and more logic added