Bug fix + code refactory