Improve locating scope open/close for functions