finally got my solution working with a named function