It's pretty inefficient, but I've modified your code