Fixed a bug in the code to protect from writing beyond screen memory

Change-Id: I60ca84eab547e9be1323e950b54e332cff6dad26