Refactor long functions