could you add another function to wrap all of these code?
This function is very complex for me to grasp