Refactoring and fix offscreen cursor