Improve performance
Minor fix of applying palette
Strip extra malloc
Allocate cache table only once to improve performance
Minor fixes