Fix gradient computation and improve test coverage (fix #16)