bugfixes and more defensive protection