Fix issue with token expansion, make general bug fixes