Move grid code into its own class.

Preparation for supporting different types of grids.
Create particle_inserter.
Add multithreading support
Tidy up remaining code