organized code and added lots of clarifying comments