Added comments, renamed a few things for clarity