Initial working version, although has bugs.