Cleaned up and added some tests