Corrected some timing problems and simplified the determination of port direction.