finished code v3.0