Added more comments and did some restructuring