massive code cleanup; updated docblocks and comments