Fixed math error in environment, made program runnable from bash script again, de-spaghettified code a bit more