finished code, now for debugging