Move some codes out of template