Functional version. Very slow and the alignment is yet to be tested. Some warnings have to be removed too.