Some refactoring, cleanup