Some refactoring, removing unused code