added C# API, fixed some bugs