Fixed a bug in the cd function, also simplified significantly the cd function, and added comments