Many changes and fixes (see diff)