restructured complete code, not yet working but close