Functional Code

after all debugs and tests, to be divided into modules.