After Demetrius captured Macedon, Athens became allied with Ptolemaic Egypt in an effort to gain its independence from Demetrius, and with Ptolemaic troops they managed to rebel and defeat Macedon in 287, though the Piraeus remained garrisoned.