fix Issue #312 + gif optimize improvement

I've reorganize gif optimize, please check it. I've run the tests and it seems to work properly.
Move a lot of code in and out of if-statements but logic should have remained the same.

The only real change it, that optimize still tries to optimize even if a small palette is found or added via parameter.