Added error checking.
Added result correctness checking.
Changed long to int so that it's the same for 32 and 64 bit machines.
Changed global wgs to be a multiple of the local one.