Hopefully made the utility functions re-entrant.
Added basic unit testing framework and some initial tests.