Partially implements #569, thx @lonvia