Some improvements that I've made when adapting it for my use.