Clean up and rewrite of built-in functions