Refactor services code