refactord main routine, and implement process management builtins