refactor and use mako template