refractor and cleanup