Fix a couple of bugs.
Move tests to separate file.
Misc. fixes.