Massively updated.

First version was from night programming challenge. I was tired and
there was only little time for the challenge.

That is my excuse, why the code was so shitty as it was.