Rewrite with bugfixes.
Rewrite with bugfixes.

at7/ensembl,danstaines/ensembl,james-monkeyshines/ensembl,at7/ensembl,at7/ensembl,muffato/ensembl,james-monkeyshines/ensembl,mjg17/ensembl,muffato/ensembl,Ensembl/ensembl,danstaines/ensembl,james-monkeyshines/ensembl,james-monkeyshines/ensembl,mjg17/ensembl,muffato/ensembl,Ensembl/ensembl,at7/ensembl,muffato/ensembl,willmclaren/ensembl,willmclaren/ensembl,mjg17/ensembl,Ensembl/ensembl,danstaines/ensembl,Ensembl/ensembl,mjg17/ensembl