Several fixes and improvements

The commits should be self describing. Any comment let me now.