merged code and tested on win7