Two-phase galaxy formation