Added comments and made parameters const and pass by reference where appropriate