Clean up unused code, improve tests