Code

Unit tests and a few fixes.