C++ code from when I was in college :0 A little bit embarrassing but oh well...