Massive mining code refactor.