added comments, preallocation, edited for loop