Fixed issue with mining process not being properly killed. Reorganized code a bit.