Now all done. Needs testing though.