Fixes a bug with ITE, fixes tests and refactors