It is very difficult to determine what it is in your program that is not working properly when you don’t explain what the function is supposed to do or what arguments it takes and only give us pseudo code. The difficult parts in your program is not this pseudo code, but how every function that is called in this code is implemented.