Finished porting Matt's Cal Code