improved alignment code