factor code and simplified prototypes of functions