Redone

Sorry, this has taken longer then I expect as the code had gotten
unreadable and it was easier to start again.