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.

Revert chmod on files to original permission

Removed trailing white spaces on files
Fixing push conflicts
Reverse file permission to original permission.