Split out into seperate files

This involved some modifications all-round.