finished code v2.0