Fixed tests for the port and improved the code