Adding documentation and cleaning up the code a little for readability