pica: code cleanup and refactoring