refact code, move script out from template index