Use static keyword to reduce code size and increase speed

I have inlined all functions that is only used once
Get rid of a lot of unnecessary global variables