With the help of the user ElliotB this issue has been resolved, below is a picture of the finished function and solution of my problem.