Lots of changes and bug fixes