fast & generic with templates, memory leak fix