Ported over code from dries007/FEcapes