Move internal miner to separate miner.cpp module

Mostly code movement, with a few bug fixes in a separate commit.