significant template improvements
separated io handling