refactored the code and fixed text positioning