main program and helper functions