Updated with changes with help from Dan Allen and Andress Almiray