Compared to the python version, this also does type checking and error handling, so it's slightly longer, however, it's still comfortably under 500 lines.