A lot of fixes and cleanup