Please excuse code quality -- making it work, then making it pretty.