extracting some functions, so that it is easier to reuse