Put common code into new functions

The construction of the different matrices happens now in separate
files that can be reused.