Split out code into new files. Fixes #415 (already fixed but adding comment now)