Made script more POSIX-compliant, also some cosmetic cleanups.