@nikitsaraf this function looks like it works but it is pretty ugly, see if you can refactor.