Heavy correction, refactoring and extension. Still incomplete