cleanup code, add more tests