Ported code from Activity to fragments