clean up code, improve tests