Lots of changes and bug fixes

Lots of changes and bug fixes