rewrote the entire thing as a bash script for speed