Adding the template, fixed some logic issues