fixed all logic. still needs documentation