first commit of draft status.
Tried to comment as much as possible and not the places that still need work.
Still a lot to do before this gives the desired output.