Stole some ideas from https://raw.githubusercontent.com/rsolomakhin/prep-image/master/prep-image.sh