Added more tests and moved certain error handling into private functions