refactor a lot of things in main code