Fix test issues, clean up code