Complete rewrite of the BAM-related programs