Improved the sexp handling functions

Proper top-level sexp detection
Reduced the functions by using simpler logic