improve script with auto detect configuration