Improved sexp handling & function refactoring