Various improvements

More error handling and fixes