Bug fix & refactoring