Fix bug that made calculations return negative numbers for off-screen elements