Refine CAS Writing code