Updating exercise to use apply function.