Fixed errors and added a few tests