added some comments to better document the subroutines.