Improved Code

Improved code performance by using StringBuilder for appending
characters, and by removing some redundancies in the code.