Make a statement more clearly
Remove a redundant pre-condition checking for addLineToScrollback
Add braces for some if statements
Add some check to prevent accessing out-of-bound data