Fixed bug where creation of directory failed when using ~ in path.

See http://stackoverflow.com/a/13168037
Minor refactoring

Removed some unnecessary duplication of code plus fixed usage of mkdir.
Doesn’t make sense to create directory in two steps when using -p. :)