updated code with lots of helpful comments