bit fix
extract part of main processing to function