Major fixes and refactoring. More tests. Basic example working.