Refactoring and tidyup of HscMain and related things (also fix #1666)