Refactoring + optimization