cleanup code, add tests