Modified to improve part of stability, gradient checks still passing.
sympykern kern_tests now passing, code is inefficient but should be numerically stable.