move variable decls to function tops, add /bin/sh check