Updated implementation from OpenTLD repository.

Mainly memory leaks, but some parameters fixes as well.