finished main implementation