improvments

while working with this code a come along some flaws which fixes i would like to share with you.
each fix is described in the commit message.

have fun!