Refactored main(), fixed stage 2 so that escape sequences aren't broken