Fixed main, added cleanup and some fixes