@szeiger: updated as suggested (but using a shared helper function instead of copy&paste)