fixed error messages for dimension checking
+ some refactoring