Port from bash to sh. Possible edge cases not tested with this script.