Major API cleanup and rewrite