Added Windows support for bgd.dll and fixed a condition that could cause segmentation fault.

Changed the code to be more resemble to the original branch.

Reverse file permission to original permission.

Squash last 3 commits.