Heavy refactoring / optimization