General improvements and bug-fixes

Fixes several command errors, attempts to improve some horrible code
(e.g. overuse of Singleton pattern) but incomplete.