Refactoring, better error messages