Added unit tests. Fixed some small input validation problems.