Removed all bashism and added POSIX compatibility