Moved apply code out of the try-catch