Bugfix: Issue #4

1. Fixed the bug.
2. Code cleanup and optimisation.