Refactor plugin code