Fixed some bugs, extended template class to allow callback instead of stdout