somewhat finished code. not tested yet.