refactored path handling in template. Cleaner now, but may not yet be optimal.