Added Integration tests. Fixed a number of bugs.