refactored text rendering code

refactored text rendering code