Nice. We should use this function also in oq-engine instead of having the same logic replicated in different places.