Last batch of refactoring + some optimizations \o/