First initial wokring version. Needs restructuring.