Major overhaul, removed bash dependence.