Refactor typecasting functions
Rename cps translation functions
Add comments and tidy up code