Refactoring

BTW, I made debug build really slow. Though release build benefits of
all the optimizations.