Replace the C extension with a much better implementation

Special thanks to @nobu!