Rewrite the most code for performance