Some refactoring, better error support