Improved internal handling of passed data, added some comments, cleaned up a few bits.