split main function and tidy up warnings