Moved apply to the function to fit closer with my previous answer