finished the code from the demo