Correct program logic and resize buffer