Fixes stack overflow in certain cases