Closes #11
Refactored the code to fix the bug added in the previous fix on SHA:c7ff6a0ee6616d12cb7c536b7c7153b77c4fd7b8