Some rewrite and performance increase