Refactor and bugFix