Adding unit tests and fixing errors

Created unit tests.
Fixed errors based on unit tests.