added test cases, hardened implementation