Refactor legacy codes