Refactored and implemented CD-K and gradient calculation. Have yet to write unittests.