Shell: some refactoring and Windows hack

Ref #36
Ref #498