Kevin final commit

Debugged some issues with the indexing of arrays and the order of
function calls. Works very well.