Few more spots that need template parameter

There still were a few "int" that needed to be converted to the template parameter. Also turned the "=="" comparison into comparing absolute value with 1.e-12 to make it work for double and integer as template parameter
use new() instead of malloc()

Avoid mixing new() and malloc() to allocate memory. Now consistently uses new() througout