all the code (WITH mainframe)