Refactored and commented code. Still needs to calculate the parameters for the DST