Fix #105. Refactored code make the implementation cleaner.