updated code from prototype to final