Huge amount of fixes and a test script