Fully updated code

Full updated code that has been tested.