Fixed errors and added unittesting