Bug fixes. Improve coding style and generalization.