I didn't test (or even try to build) this, but here's how you can cut out a lot of boilerplate.