added 5th condition for cd (no 2nd argument should result in cwd being HOME). allocates mem as Rane's code did (more efficient)