Fixes for compatibility with 3.x, and cleanup