rewrite using template, less code