get end detection working and iron out some bugs