moved main stuff into function to ease debugging