Tests and fix for class toggling

Changes in this file are from #646 .