Some refactoring, other useful changes