finished code at last