added a whole bunch of commenting, logging improvements and testing