tidying up main code