Added apply statements instead of for loops.