merged some code by Nedko Arnaudov and updated it to current interface, fixed some possible segfaults (uninited pointers)