Added comments, fixed some logic, viewer updated