code refactor + bug fix