restructuring and adding textwriting code