Rewrote the lib in a more idiomatic way.