I think I've finally found the problem with my function