Added optimalizations in speed when using template