lots of validation checking and bugfixes