Merge with KS's code