Some refactoring, new functions