Detailed comments added

Each segment of both functions have been commented to explain what they
do.