Remove unused code; improve tests