clear up duplicate code, add comments