Reimplementation of token bucket & code optimizations