Solution for programming assignment 2

That was a lot of thinking, minimal editing