fixed lots of bugs in main