Major changes

Hi Matt, I was able to fix the two remaining issues, so I'm sending the the big commit. Cheers!