Refactored and cleaned a bit the template implementation