Lots of modifications and fixes