major rewrite for 5.x