unsafe reading. still buggy
fix one bug
forgot to for end of output