fix cursor placement; code cleanup