Fixed a small logging bug, added comments to the code.