Added finalizer to avoid memory leaks; made code compatible with  Intel compilers