bug fix & refactor