Fixed a couple of issues and added some tests