Fixed the bug in PIO that Saeed found + added tests to verify bugfix