Complete, working code. Just needs comments.