Some improvements and bugs fixing.

This pull request also fixed Issue #24