add tests and fix all bugs