refactored for easier use with other template engines