Lots of work on the main code