Reworked everything. Works on bash 4.1 and up.