code refactoring + optimization