Refix for that function. Add asserts to check