Added comments and corrected some bugs - some still remain