Final version- everything seems to work nicely.

Still some code cleaning required