Added unit testing, corrected a fiew bugs while I was at it.