fixed incorrect signs, int truncation issue, and added derivative implementation