Resolved issue #8 and #11, and did some additional code refactoring