fixed various bugs, implemented gradient checking