Introduce handlers

Implements #135 with a more generic approach.