added test coverage and fixed some bugs