Code cleanup, synchronisation fixes, and output to filtered bam now appears to work with correct header