Very ugly HW2 solution. Needs cleanup!