As suggested by Henrik I changed it to use a delegate function, which reduces code duplication and makes things cleaner