Bunch of refactoring, optimizations and (mostly unused) extensions