Refactor part of activity code