Restructured shell, fixed some bugs.