This function should work, although it is not tested
work in progress