added call to apply, and clarified and commented code