Added port.h and some refactoring.