Refactored the mining code