[Fix][Refactor] Some bug fixes and minor refactor