Thanks @a1291762 for refactoring my crappy bash away!