Added lapply function to remove for-loops