Added a few comments explaining the functions and implementation.