fixing data structure and template logic