Fixing my terrible shell script skills before anyone notices. I totally didnt rip this code off SO.