replaced all calls to sapply with vapply