refactor complicated method