Merge code from acid

Adds in code given to me by Acidity. 

All credit to him