Make the code DRY

**Note**: This is _untested_ code. I'm not expecting this to get pulled (now).

I've updated the code to be DRY, because the code for the different entities was basically the same all over.

If you like the idea we can try to get this pulled _after_ the tests (#9) are in place.