Rewritten in a more general way for main repo