Tweaked some code and added some tests