Fixed some bugs. Added coverage tests