merge functional code

First commit of functional code.