Incorporating a bunch more functions I developed elsewhere.