Several improvements in loops and conditionals

I've tried to make the code more concise, readable and more ruby-style.
I hope you like.

I tried to break the commits in very small chunks to be more understandable.

The tests are passing clearly.